{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "44722111fdaf"
   },
   "source": [
    "# Getting started with Honeywell and Cirq on Azure Quantum\n",
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://quantumai.google/cirq/hardware/azure-quantum/getting_started_honeywell\"><img src=\"https://quantumai.google/site-assets/images/buttons/quantumai_logo_1x.png\" />View on QuantumAI</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/main/docs/hardware/azure-quantum/getting_started_honeywell.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/colab_logo_1x.png\" />Run in Google Colab</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/main/docs/hardware/azure-quantum/getting_started_honeywell.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/github_logo_1x.png\" />View source on GitHub</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/hardware/azure-quantum/getting_started_honeywell.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/download_icon_1x.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "a14c9d445fba"
   },
   "source": [
    "This notebook shows how to send a basic quantum circuit to a Honeywell target via Azure Quantum.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "- To work in Azure Quantum, you need an Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/).\n",
    "- Create an Azure Quantum workspace and enable Honeywell. For more information, see [Create an Azure Quantum workspace](https://docs.microsoft.com/azure/quantum/quickstart-microsoft-qc?pivots=platform-honeywell#create-an-azure-quantum-workspace)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "d48b381b86ed"
   },
   "source": [
    "First, install `azure-quantum` with the Cirq dependencies:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "96e68f63b1d0"
   },
   "outputs": [],
   "source": [
    "!pip install 'azure-quantum[cirq]' --quiet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4f352e5d5861"
   },
   "source": [
    "## Connecting to the Azure Quantum service\n",
    "\n",
    "To connect to the Azure Quantum service, find the resource ID and location of your Workspace from the Azure Portal here: https://portal.azure.com. Navigate to your Azure Quantum Workspace and copy the values from the header.\n",
    "\n",
    "<img src=\"azure-quantum-resource-id.png\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "4a030f6b1ff5"
   },
   "outputs": [],
   "source": [
    "from azure.quantum.cirq import AzureQuantumService\n",
    "\n",
    "service = AzureQuantumService(\n",
    "    resource_id=\"\", location=\"\", default_target=\"honeywell.hqs-lt-s1-apival\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "904776cfe71c"
   },
   "source": [
    "### List all Honeywell targets\n",
    "\n",
    "You can now list all the targets that you have access to, including the current queue time and availability. To only return the Honeywell provider's targets, you can specify the optional `provider_id` input argument:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "aea4f1751f39"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Target name=\"honeywell.hqs-lt-s1\", avg. queue time=0 s, Unavailable>,\n",
      " <Target name=\"honeywell.hqs-lt-s1-apival\", avg. queue time=0 s, Available>]\n"
     ]
    }
   ],
   "source": [
    "service.targets(provider_id=\"honeywell\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "0959933b3ba6"
   },
   "source": [
    "To read more about the Honeywell API validator and QPU specifications such as number of qubits, connectivity, system time scales and fidelities, you can check out the [Honeywell Provider Reference](https://docs.microsoft.com/azure/quantum/provider-honeywell)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "e4bd1fe16251"
   },
   "source": [
    "## Run a simple circuit\n",
    "\n",
    "Now, let's create a simple Cirq circuit to run."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "46b31e5f5276"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"overflow: auto; white-space: pre;\">0: ───H───@───M(&#x27;b&#x27;)───\n",
       "          │   │\n",
       "1: ───────X───M────────</pre>"
      ],
      "text/plain": [
       "0: ───H───@───M('b')───\n",
       "          │   │\n",
       "1: ───────X───M────────"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cirq\n",
    "\n",
    "q0, q1 = cirq.LineQubit.range(2)\n",
    "circuit = cirq.Circuit(\n",
    "    cirq.H(q0),  # Hadamard\n",
    "    cirq.CNOT(q0, q1),  # CNOT\n",
    "    cirq.measure(q0, q1, key='b'),  # Measure both qubits into classical register \"b\"\n",
    ")\n",
    "circuit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "fc61d090b25b"
   },
   "source": [
    "You can now run the program via the Azure Quantum service and get the result. The following cell will submit a job that runs the circuit with 100 repetitions, wait until the job is completed and return the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "ed8cc1e2a22b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "......."
     ]
    }
   ],
   "source": [
    "result = service.run(program=circuit, repetitions=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "f2ccb7c043c4"
   },
   "source": [
    "This returns a `cirq.Result` object. Note that the API validator only returns zeros."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "id": "710784ea4e12"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b=0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n"
     ]
    }
   ],
   "source": [
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "60b2570d3341"
   },
   "source": [
    "## Asynchronous workflow using Jobs\n",
    "\n",
    "For long-running circuits, it can be useful to run them asynchronously. The `service.create_job` method returns a `Job`, which you can use to get the results after the job has run successfully."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "id": "122d8679e03a"
   },
   "outputs": [],
   "source": [
    "job = service.create_job(program=circuit, repetitions=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "b7315b371f77"
   },
   "source": [
    "To check on the job status, use `job.status()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "id": "a92c9d72435b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Waiting'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "job.status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "a5c2cebc3a51"
   },
   "source": [
    "To wait for the job to be done and get the results, use the blocking call `job.results()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "id": "6dfec987654e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".......{'m_b': ['00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00']}\n"
     ]
    }
   ],
   "source": [
    "result = job.results()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "d3d658b2d172"
   },
   "source": [
    "This returns a dictionary of lists. The dictionary keys are the name of the classical register prepended with `\"m_\"`, and the values are a list of bit strings that are measured for each repetition. Since here, in the `cirq` program measures the results for both qubits 0 and 1 into a register `\"b\"`, you can access the list of measurement results for those qubits with key `\"m_b\"`. Since here you ran the program with 100 repetitions, the length of the list should be 100:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "id": "bec675a56769"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(result[\"m_b\"])"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "getting_started_honeywell.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
